import { Injectable } from '@nestjs/common';
import { CreateLinkDto } from './dto/create-link.dto';
import { UpdateLinkDto } from './dto/update-link.dto';
import { PrismaService } from '../prisma/prisma.service'

@Injectable()
export class LinkService {
    constructor(private readonly prisma: PrismaService) { }

    create(createLinkDto: CreateLinkDto) {
        return this.prisma.link.create({
            data: createLinkDto
        })
    }

    async findAll(query: { status?: number, title: string, pageIndex: number, pageSize: number }) {
        const list = await this.prisma.link.findMany({
            where: {
                status: query.status,
                title: { contains: query.title },
            },
            skip: (query.pageIndex - 1) * query.pageSize,
            orderBy: { createTime: 'desc' },
            take: query.pageSize
        })
        const total = await this.prisma.link.count({
            where: {  
                status: query.status,
                title: { contains: query.title },
            }
        })
        return { list, total }
    }

    findOne(id: number) {
        return this.prisma.link.findUnique({
            where: { id }
        })
    }

    update(id: number, updateLinkDto: UpdateLinkDto) {
        return this.prisma.link.update({
            where: { id },
            data: updateLinkDto
        })
    }

    remove(id: number) {
        return this.prisma.link.delete({
            where: { id }
        })
    }
}
